<?php

namespace cms\modules\v1\controllers;

use cms\modules\v1\models\Home;
use cms\modules\v1\models\Upload;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;
use Yii;

/**
 * 首页控制器
 * Class HomeController
 * @package cms\modules\v1\controllers
 */
class HomeController extends DefaultController
{

    public $modelClass = 'cms\modules\v1\models\Home';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),
                'header' => 'access-token',
                'optional' => [
                    "upload","upload-for-wang"
                ]
            ]
        ]);
    }

    /**
     * 获取首页信息-后台
     * @return array
     */
    public function actionCardData() {
        if (\Yii::$app->request->isGet) {
            return (new Home())->getHomeCardData();
        }
    }

    public function actionUpload($imageUrl = '') {
        if (Yii::$app->request->isPost) {
//            return (new Upload())->uploadToQiniu();
            return (new Upload())->uploadToAli();
        }
        if (Yii::$app->request->isDelete) {
            return (new Upload())->deleteImage($imageUrl);
        }
    }

    public function actionUpToken() {
        if (\Yii::$app->request->isPost) {
            return (new Upload())->getUpToken();
        }
    }

    /**
     * 上传编辑器图片
     * @return array
     */
    public function actionUploadForWang() {
        if (\Yii::$app->request->isPost) {
            return (new Upload())->uploadForWangAli();
//            return (new Upload())->uploadForWang();
        }
    }
}
